home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / Make / source / signame.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-23  |  7.9 KB  |  313 lines

  1. /* Convert between signal names and numbers.
  2.    Copyright (C) 1990, 1992, 1993, 1995, 1996 Free Software Foundation, Inc.
  3.    This file is part of the GNU C Library.
  4.  
  5.    The GNU C Library is free software; you can redistribute it and/or
  6.    modify it under the terms of the GNU Library General Public License as
  7.    published by the Free Software Foundation; either version 2 of the
  8.    License, or (at your option) any later version.
  9.  
  10.    The GNU C Library is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.    Library General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU Library General Public
  16.    License along with the GNU C Library; see the file COPYING.LIB.  If not,
  17.    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  18.    Boston, MA 02111-1307, USA.  */
  19.  
  20. #ifdef HAVE_CONFIG_H
  21. #include <config.h>
  22. #endif
  23.  
  24. #include <stdio.h>
  25. #include <sys/types.h>        /* Some systems need this for <signal.h>.  */
  26. #include <signal.h>
  27.  
  28. #ifdef HAVE_STRING_H
  29. #include <string.h>
  30. #endif
  31.  
  32. /* Some systems declare `sys_siglist in <unistd.h>; if
  33.    configure defined SYS_SIGLIST_DECLARED, it may expect
  34.    to find the declaration there.  */
  35. #ifdef HAVE_UNISTD_H
  36. #include <unistd.h>
  37. #endif
  38.  
  39.  
  40. /* Some systems do not define NSIG in <signal.h>.  */
  41. #ifndef    NSIG
  42. #ifdef    _NSIG
  43. #define    NSIG    _NSIG
  44. #else
  45. #define    NSIG    32
  46. #endif
  47. #endif
  48.  
  49. #if !__STDC__
  50. #define const
  51. #endif
  52.  
  53. #include "signame.h"
  54.  
  55. #ifndef HAVE_SYS_SIGLIST
  56. /* There is too much variation in Sys V signal numbers and names, so
  57.    we must initialize them at runtime.  */
  58.  
  59. static const char undoc[] = "unknown signal";
  60.  
  61. const char *sys_siglist[NSIG];
  62.  
  63. #else    /* HAVE_SYS_SIGLIST.  */
  64.  
  65. #ifndef SYS_SIGLIST_DECLARED
  66. extern char *sys_siglist[];
  67. #endif    /* Not SYS_SIGLIST_DECLARED.  */
  68.  
  69. #endif    /* Not HAVE_SYS_SIGLIST.  */
  70.  
  71. /* Table of abbreviations for signals.  Note:  A given number can
  72.    appear more than once with different abbreviations.  */
  73. typedef struct
  74.   {
  75.     int number;
  76.     const char *abbrev;
  77.   } num_abbrev;
  78. static num_abbrev sig_table[NSIG*2];
  79. /* Number of elements of sig_table used.  */
  80. static int sig_table_nelts = 0;
  81.  
  82. /* Enter signal number NUMBER into the tables with ABBREV and NAME.  */
  83.  
  84. static void
  85. init_sig (number, abbrev, name)
  86.      int number;
  87.      const char *abbrev;
  88.      const char *name;
  89. {
  90. #ifndef HAVE_SYS_SIGLIST
  91.   sys_siglist[number] = name;
  92. #endif
  93.   sig_table[sig_table_nelts].number = number;
  94.   sig_table[sig_table_nelts++].abbrev = abbrev;
  95. }
  96.  
  97. void
  98. signame_init ()
  99. {
  100. #ifndef HAVE_SYS_SIGLIST
  101.   int i;
  102.   /* Initialize signal names.  */
  103.   for (i = 0; i < NSIG; i++)
  104.     sys_siglist[i] = undoc;
  105. #endif /* !HAVE_SYS_SIGLIST */
  106.  
  107.   /* Initialize signal names.  */
  108. #if defined (SIGHUP)
  109.   init_sig (SIGHUP, "HUP", "Hangup");
  110. #endif
  111. #if defined (SIGINT)
  112.   init_sig (SIGINT, "INT", "Interrupt");
  113. #endif
  114. #if defined (SIGQUIT)
  115.   init_sig (SIGQUIT, "QUIT", "Quit");
  116. #endif
  117. #if defined (SIGILL)
  118.   init_sig (SIGILL, "ILL", "Illegal Instruction");
  119. #endif
  120. #if defined (SIGTRAP)
  121.   init_sig (SIGTRAP, "TRAP", "Trace/breakpoint trap");
  122. #endif
  123.   /* If SIGIOT == SIGABRT, we want to print it as SIGABRT because
  124.      SIGABRT is in ANSI and POSIX.1 and SIGIOT isn't.  */
  125. #if defined (SIGABRT)
  126.   init_sig (SIGABRT, "ABRT", "Aborted");
  127. #endif
  128. #if defined (SIGIOT)
  129.   init_sig (SIGIOT, "IOT", "IOT trap");
  130. #endif
  131. #if defined (SIGEMT)
  132.   init_sig (SIGEMT, "EMT", "EMT trap");
  133. #endif
  134. #if defined (SIGFPE)
  135.   init_sig (SIGFPE, "FPE", "Floating point exception");
  136. #endif
  137. #if defined (SIGKILL)
  138.   init_sig (SIGKILL, "KILL", "Killed");
  139. #endif
  140. #if defined (SIGBUS)
  141.   init_sig (SIGBUS, "BUS", "Bus error");
  142. #endif
  143. #if defined (SIGSEGV)
  144.   init_sig (SIGSEGV, "SEGV", "Segmentation fault");
  145. #endif
  146. #if defined (SIGSYS)
  147.   init_sig (SIGSYS, "SYS", "Bad system call");
  148. #endif
  149. #if defined (SIGPIPE)
  150.   init_sig (SIGPIPE, "PIPE", "Broken pipe");
  151. #endif
  152. #if defined (SIGALRM)
  153.   init_sig (SIGALRM, "ALRM", "Alarm clock");
  154. #endif
  155. #if defined (SIGTERM)
  156.   init_sig (SIGTERM, "TERM", "Terminated");
  157. #endif
  158. #if defined (SIGUSR1)
  159.   init_sig (SIGUSR1, "USR1", "User defined signal 1");
  160. #endif
  161. #if defined (SIGUSR2)
  162.   init_sig (SIGUSR2, "USR2", "User defined signal 2");
  163. #endif
  164.   /* If SIGCLD == SIGCHLD, we want to print it as SIGCHLD because that
  165.      is what is in POSIX.1.  */
  166. #if defined (SIGCHLD)
  167.   init_sig (SIGCHLD, "CHLD", "Child exited");
  168. #endif
  169. #if defined (SIGCLD)
  170.   init_sig (SIGCLD, "CLD", "Child exited");
  171. #endif
  172. #if defined (SIGPWR)
  173.   init_sig (SIGPWR, "PWR", "Power failure");
  174. #endif
  175. #if defined (SIGTSTP)
  176.   init_sig (SIGTSTP, "TSTP", "Stopped");
  177. #endif
  178. #if defined (SIGTTIN)
  179.   init_sig (SIGTTIN, "TTIN", "Stopped (tty input)");
  180. #endif
  181. #if defined (SIGTTOU)
  182.   init_sig (SIGTTOU, "TTOU", "Stopped (tty output)");
  183. #endif
  184. #if defined (SIGSTOP)
  185.   init_sig (SIGSTOP, "STOP", "Stopped (signal)");
  186. #endif
  187. #if defined (SIGXCPU)
  188.   init_sig (SIGXCPU, "XCPU", "CPU time limit exceeded");
  189. #endif
  190. #if defined (SIGXFSZ)
  191.   init_sig (SIGXFSZ, "XFSZ", "File size limit exceeded");
  192. #endif
  193. #if defined (SIGVTALRM)
  194.   init_sig (SIGVTALRM, "VTALRM", "Virtual timer expired");
  195. #endif
  196. #if defined (SIGPROF)
  197.   init_sig (SIGPROF, "PROF", "Profiling timer expired");
  198. #endif
  199. #if defined (SIGWINCH)
  200.   /* "Window size changed" might be more accurate, but even if that
  201.      is all that it means now, perhaps in the future it will be
  202.      extended to cover other kinds of window changes.  */
  203.   init_sig (SIGWINCH, "WINCH", "Window changed");
  204. #endif
  205. #if defined (SIGCONT)
  206.   init_sig (SIGCONT, "CONT", "Continued");
  207. #endif
  208. #if defined (SIGURG)
  209.   init_sig (SIGURG, "URG", "Urgent I/O condition");
  210. #endif
  211. #if defined (SIGIO)
  212.   /* "I/O pending" has also been suggested.  A disadvantage is
  213.      that signal only happens when the process has
  214.      asked for it, not everytime I/O is pending.  Another disadvantage
  215.      is the confusion from giving it a different name than under Unix.  */
  216.   init_sig (SIGIO, "IO", "I/O possible");
  217. #endif
  218. #if defined (SIGWIND)
  219.   init_sig (SIGWIND, "WIND", "SIGWIND");
  220. #endif
  221. #if defined (SIGPHONE)
  222.   init_sig (SIGPHONE, "PHONE", "SIGPHONE");
  223. #endif
  224. #if defined (SIGPOLL)
  225.   init_sig (SIGPOLL, "POLL", "I/O possible");
  226. #endif
  227. #if defined (SIGLOST)
  228.   init_sig (SIGLOST, "LOST", "Resource lost");
  229. #endif
  230. #if defined (SIGDANGER)
  231.   init_sig (SIGDANGER, "DANGER", "Danger signal");
  232. #endif
  233. #if defined (SIGINFO)
  234.   init_sig (SIGINFO, "INFO", "Information request");
  235. #endif
  236. #if defined (SIGNOFP)
  237.   init_sig (SIGNOFP, "NOFP", "Floating point co-processor not available");
  238. #endif
  239. }
  240.  
  241. /* Return the abbreviation for signal NUMBER.  */
  242.  
  243. char *
  244. sig_abbrev (number)
  245.      int number;
  246. {
  247.   int i;
  248.  
  249.   if (sig_table_nelts == 0)
  250.     signame_init ();
  251.  
  252.   for (i = 0; i < sig_table_nelts; i++)
  253.     if (sig_table[i].number == number)
  254.       return (char *)sig_table[i].abbrev;
  255.   return NULL;
  256. }
  257.  
  258. /* Return the signal number for an ABBREV, or -1 if there is no
  259.    signal by that name.  */
  260.  
  261. int
  262. sig_number (abbrev)
  263.      const char *abbrev;
  264. {
  265.   int i;
  266.  
  267.   if (sig_table_nelts == 0)
  268.     signame_init ();
  269.  
  270.   /* Skip over "SIG" if present.  */
  271.   if (abbrev[0] == 'S' && abbrev[1] == 'I' && abbrev[2] == 'G')
  272.     abbrev += 3;
  273.  
  274.   for (i = 0; i < sig_table_nelts; i++)
  275.     if (abbrev[0] == sig_table[i].abbrev[0]
  276.     && strcmp (abbrev, sig_table[i].abbrev) == 0)
  277.       return sig_table[i].number;
  278.   return -1;
  279. }
  280.  
  281. #ifndef HAVE_PSIGNAL
  282. /* Print to standard error the name of SIGNAL, preceded by MESSAGE and
  283.    a colon, and followed by a newline.  */
  284.  
  285. void
  286. psignal (signal, message)
  287.      int signal;
  288.      const char *message;
  289. {
  290.   if (signal <= 0 || signal >= NSIG)
  291.     fprintf (stderr, "%s: unknown signal", message);
  292.   else
  293.     fprintf (stderr, "%s: %s\n", message, sys_siglist[signal]);
  294. }
  295. #endif
  296.  
  297. #ifndef HAVE_STRSIGNAL
  298. /* Return the string associated with the signal number.  */
  299.  
  300. char *
  301. strsignal (signal)
  302.      int signal;
  303. {
  304.   static char buf[] = "Signal 12345678901234567890";
  305.  
  306.   if (signal > 0 || signal < NSIG)
  307.     return (char *) sys_siglist[signal];
  308.  
  309.   sprintf (buf, "Signal %d", signal);
  310.   return buf;
  311. }
  312. #endif
  313.